/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 12:12:55 PM
 */
package carrello.model.association;

import carrello.model.entities.*;
import java.io.Serializable;
import javax.persistence.*;
import static carrello.util.EntityManagerUtil.getEntityManager;

@Entity
@Table(name = "AttributeValue")
@AssociationOverrides({
@AssociationOverride(name="pkey.product", joinColumns = @JoinColumn(name="product_id")),
@AssociationOverride(name="pkey.attribute", joinColumns = @JoinColumn(name="attribute_id"))
})
public class AttributeValue {
    
    @EmbeddedId
    private AttributeValuePK pkey = new AttributeValuePK();

    public AttributeValue() {
    }
    
    public void setPkey(AttributeValuePK pkey)
    {
        this.pkey = pkey;
    }
    
    public AttributeValuePK getPkey()
    {
        return this.pkey;
    }
    
    @Column(name = "value")
    private String value;
    
    @Column(name="product_id", nullable=false, updatable=false, insertable=false)
    private Long product;
    
    @Column(name="attribute_id", nullable=false, updatable=false, insertable=false)
    private Long attribute;
    
    public AttributeValue(AttributeValuePK avPK, String value)
    {
        this.pkey = avPK;
        this.value = value;
        
    }
    public void setProduct(Product product)
    {
        pkey.setProduct(product);
    }
    
    public Product getProduct()
    {
        return pkey.getProduct();
    }
    
    public void setAttribute(Attribute attribute)
    {
        pkey.setAttribute(attribute);
    }
    
    public Attribute getAttribute()
    {
        return pkey.getAttribute();
    }
    
    public void setValue(String value)
    {
        this.value = value;
    }
    
    public String getValue()
    {
        return this.value;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
 
        AttributeValue that = (AttributeValue) o;
 
        if (getPkey() != null ? !getPkey().equals(that.getPkey()) : that.getPkey() != null) return false;
 
        return true;
    }
 
    public int hashCode() {
        return (getPkey() != null ? getPkey().hashCode() : 0);
    }
    /**
     * delete the attribute values of given product
     * @param p Given Product
     */
    public void delete(Attribute a)
    {
        getEntityManager().removeAll(a.getAttributeValue());
    }
    
    public void delete(Product p)
    {
        getEntityManager().removeAll(p.getAttributeValue());
    }
}
